package org.apache.webot;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.ParserException;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Xrnd on 2015/9/15.
 */
public class Main {
    private final static String html = "http://www.w3school.com.cn";
    private final static String encoding = "UTF-8";

    private final static int threads = 10;

    private final static String[] pfx = new String[] {
        "http://",
        "https://"
    };

    private static ReentrantLock lock = new ReentrantLock();
    private static Set<String> tbdo = new HashSet<String>(1);
    private static Set<String> done = new HashSet<String>(0);

    private String home = null;

    public static void main (String[] args) throws Exception {
        String home = html;
        if (null != args && args.length >= 2)
            home = args[1];
        new Main (home);
    }

    private Main (String url) throws Exception {
        int pos = -1;
        for (int i = 0; i < pfx.length; ++ i) {
            if (url.startsWith(pfx[i])) {
                pos = url.indexOf('/', pfx[i].length());
                if (pos > 0) {
                    home = url.substring(0, pos);
                } else {
                    home = url;
                }
                break;
            }
        }

        if (null == url || url.length() <= 0)
            throw new Exception("invalid home page!");

        add(url);

        for (int i = 0; i < threads; ++ i) {
            es.execute(new Loader());
        }
    }

    private final static ExecutorService es = Executors.newFixedThreadPool(threads);
    private static void add (String url) {
        lock.lock();
        tbdo.add(url);
        lock.unlock();
    }
    private static String get () {
        lock.lock();
        Iterator<String> it = tbdo.iterator();
        String url = null;
        if (it.hasNext()) {
            url = it.next();
            it.remove();
        }
        lock.unlock();
        return url;
    }

    private final static class Loader implements Runnable {
        public synchronized void run() {
            String url = get();
            if (null != url) {
                Parser parser = Parser.createParser(url, encoding);
                try {
                    NodeIterator it = parser.elements();
                    Node node;
                    while (it.hasMoreNodes()) {
                        node = it.nextNode();
                    }
                } catch (ParserException e) {
                    e.printStackTrace();
                }
            }
        }
    };


    private static Integer count = 0;
    private final static class Increator implements Runnable {
        public synchronized void run() {
            synchronized (count) {
                try {
                    wait();
                    count = count + 1;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    private final static class Decreator implements Runnable {
        public synchronized void run () {
            synchronized (count) {
                try {
                    wait();
                    count = count - 1;
                    if (0 >= count) {

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private final static class Shutdown implements Runnable {
        public synchronized void run () {
            try {
                wait();
                es.shutdown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
